/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */

/*Vue.transition('fade', {
    enterClass: 'fadeInLeft',
    leaveClass: 'hide'
});*/


var vm=new Vue({
  el:"#app",
  data:{
    NoticeInfo:[],
    NoticeList:{    //文档
      isLoading:true,
      pageSize:PAGESIZE,
      pageIndex:1
    },
    /*RemindInfo:[],
    RemindList:{   //话题
      isLoading:true,
      pageSize:PAGESIZE,
      pageIndex:1
    },
    PrivateLetterInfo:[],
    PrivateLetterList:{  //回答
      isLoading:true,
      pageSize:PAGESIZE,
      pageIndex:1
    },*/

    MessageCount: '',
    tabs:0,
    reqOut: false, //判断请求数据是否已完
    category:''
  },
  created:function(){
    var _self = this;

    Common.ajaxCall({ url: "user_getMessageUnreadCount", datatype: 'json', type: 'get'}, function (data) {
        if(data.error_code==0){
            _self.MessageCount = data.data[0];
        }else{
            alert(data.msg);
        }
    });

  },
  compiled: function(){
    var _self = this;
    this.loadPhoto();
    var top = $(document).scrollTop();
    $(document).scroll(function () {
      if(top!=$(document).scrollTop()){
          // 上下滚动
          var docHeight = $(document).height(),
                winHeight = $(window).height();
            if ((docHeight > winHeight)&&($(document).scrollTop() >= docHeight - winHeight)) {
                if (!_self.reqOut) {
                    _self.reqOut = true;
                    var str = '<img class="loading-img" src="images/loading.gif"></img>';
                    myLoad.pullLoad('push', str);
                    _self.loadlist(_self.tabs);

                }
            }
      }else{
          // 左右滚动
      }
      
    });
    
    $(document).on('change.inputText','.fillInput',function(){
        _self.inputText = $(this).val();
    });


    $(document).on('click','.edit',function(e){
      if($(e.target).hasClass('on')){
        $(e.target).removeClass('on');
      }else{
        $(e.target).addClass('on');
      }
    });

    $(window).scroll(function() {
        var topH = $(window).scrollTop();
        var swiperH = $('.swiper-container').height();
        if (topH > swiperH) {
            $('.pushTab').addClass('top');
            $('.tabBox').css('padding-top', $('.indexTab').outerHeight());
        } else {
            $('.pushTab').removeClass('top');
            $('.tabBox').css('padding-top', '0');
        }
    });

    _self.loadlist(_self.tabs);
  },
  ready:function(){

  },
  methods:{
    loadlist:function(tabs){
      var _self = this;
      var vModal,target;
      var url = 'user_getNoticeList';
      switch(tabs){
        case 0:  //通知
          _self.category = 'sendme';
          var defaults = {
            category:'sendme',
            pageSize:_self.NoticeList.pageSize,
            pageIndex:_self.NoticeList.pageIndex
          }
        break;
        case 1:  //提醒
          _self.category = 'mentioned';
          var defaults = {
            category:'mentioned',
            pageSize:_self.NoticeList.pageSize,
            pageIndex:_self.NoticeList.pageIndex
          } 
        break;
        case 2:  //私信
          _self.category = 'comment';
          var defaults = {
            category:'comment',
            pageSize:_self.NoticeList.pageSize,
            pageIndex:_self.NoticeList.pageIndex
          }
        break;
        case 3:  //关注
          _self.category = 'subscribe';
          var defaults = {
            category:'subscribe',
            pageSize:_self.NoticeList.pageSize,
            pageIndex:_self.NoticeList.pageIndex
          }
        break;
        case 4:  //其他
          _self.category = 'default';
          var defaults = {
            category:'default',
            pageSize:_self.NoticeList.pageSize,
            pageIndex:_self.NoticeList.pageIndex
          }
        break;
      }
      var data = $.extend(defaults,{});

      vModal = _self.NoticeList;
      target = _self.NoticeInfo;

      if(!vModal || !vModal.isLoading) return;
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: url, datatype: 'json', type: 'get', data: data}, function (data) {
          if(data.error_code==0 && data.data.length > 0){
              if(vModal.pageIndex==1){
                target.length = 0;
              }
              for(var i = 0; i < data.data.length; i++){
                target.push(data.data[i]);
              }
              _self.reqOut = false;

              //实际数据与请求的数据量比较
              if (data.data.length >= vModal.pageSize) {
                  myLoad.pullHide(); //加载后，把效果去掉
              } else {
                  _self.reqOut = true;
                  myLoad.noDate(); //暂无数据
                  vModal.isLoading = false;
              }
              
          }else{
              _self.reqOut = true;
              myLoad.noDate(); //暂无数据
              vModal.isLoading = false;
          }

          vModal.pageIndex++;
      });
      
    },
    loadPhoto:function(){
      // 轮播比例
      myJS.imgScale('.j_1', '1', '1');
      myJS.ImgFull('.loadImg','images/liby.png');
    },
    slides:function(index,e){
      var _self = this;

      _self.tabs = index;
      $(e.currentTarget).addClass('on').siblings().removeClass('on');

      _self.NoticeInfo = [];
      _self.NoticeList = {    //文档
        isLoading:true,
        pageSize:PAGESIZE,
        pageIndex:1
      }

      _self.loadlist(_self.tabs);
      
    },
    read:function(id,e){
      var _self = this;
      var href = $(e.target).parents('a').data('url');

      var data = {
        category: _self.category
      }
      Common.ajaxCall({ url: "user_setRead", datatype: 'json', type: 'get',data:data}, function (data) {
        if(data.error_code==0){
          if(_self.tabs===2){  //私信固定地址
            window.location.href = 'textArea.html?id='+ id +'&fromNotice=1&back=newsList.html';
          }else{
            if(href.indexOf('circleList')>-1){
              alert('暂不支持访问');
            }else{
              window.location.href = href+"&back=newsList.html";
            }
          }
        }else{
            alert(data.msg);
        }
      });
      
    }
  },
  watch:{
    "NoticeInfo": function(){
      this.loadPhoto();
    }
  }
});